<template>
  <div>
    <el-card class="box-card">
      <div slot="header">
        <my-bread> 粉丝管理</my-bread>
      </div>

      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="粉丝列表" name="first">
          <div v-for="item in fanslist" :key="item.name" class="block">
            <el-avatar :size="50" :src="item.photo"> </el-avatar>
            <p>{{ item.name }}</p>
            <el-button size="mini" type="success">+关注</el-button>
          </div>
          <el-pagination
            @current-change="currentchange"
            :page-size="objectFrom.per_page"
            :current-page="objectFrom.page"
            background
            layout="prev, pager, next"
            :total="total"
          >
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="粉丝画像" name="second">
          <div ref="dom" style="height:400px;width:600px"></div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  data() {
    return {
      // 默认显示用画像
      activeName: "second",
      // 分页总数
      total: 0,
      // 接收粉丝数据
      fanslist: [],
      // 参数对象
      objectFrom: {
        page: 1,
        per_page: 24
      }
    };
  },
  // 自动执行获取信息
  created() {
    this.getcontent();
  },
  mounted() {
    this.initBar();
  },
  methods: {
    // 初始化柱状图
    async initBar() {
      const myChart = echarts.init(this.$refs.dom);
      const option = {
        color: ["#3398DB"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            type: "category",
            // x轴坐标刻度
            data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "直接访问",
            type: "bar",
            barWidth: "60%",
            // x轴刻度对应数据
            data: [10, 52, 200, 334, 390, 330, 220]
          }
        ]
      };

      try {
        // 获取统计的数据 data.age 是粉丝的年龄分布数据 (接口不稳定，不行多刷新几次)
        const {
          data: { data }
        } = await this.$http.get("/statistics/followers");
        // arr1 是x轴刻度，arr2 是刻度对应数据
        const arr1 = [];
        const arr2 = [];
        for (const key in data.age) {
          arr1.push(key);
          arr2.push(data.age[key]);
        }
        // 修改option
        option.xAxis[0].data = arr1;
        option.series[0].data = arr2;
        // 根据option绘制
        myChart.setOption(option);
      } catch (e) {
        this.$message.error("网络繁忙，刷新下试试");
      }
    },
    // 获取当前点击分页
    currentchange(number) {
      this.objectFrom.page = number;
      this.getcontent();
    },
    // 获取粉丝数据
    async getcontent() {
      const {
        data: { data }
      } = await this.$http.get("/followers", { params: this.objectFrom });
      console.log(data);
      // 获取粉丝信息
      this.fanslist = data.results;
      // 获取总页数
      this.total = data.total_count;
    }
  }
};
</script>

<style scoped lang="less">
.block {
  padding-top: 10px;
  width: 100px;
  height: 120px;
  text-align: center;
  border: 1px dashed #ddd;
  display: inline-block;
  margin-right: 20px;
  margin-bottom: 20px;
  p {
    margin: 5px 0;
    font-size: 15px;
  }
}
</style>
